<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="deviceName">
        <el-select v-model="queryParams.deviceName" placeholder="请选择设备名称" clearable>
          <el-option v-for="dict in dict.type.device_name" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceModel" label-width="100px">
        <el-input v-model="queryParams.deviceModel" placeholder="请输入设备类型" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备负责人" prop="deviceManager" label-width="100px">
        <el-input v-model="queryParams.deviceManager" placeholder="请输入设备负责人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备保养周期" prop="deviceRound" label-width="110px">
        <el-input v-model="queryParams.deviceRound" placeholder="请输入设备保养周期" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备维保到期剩余" prop="deviceDeadline" label-width="140px">
        <el-input v-model="queryParams.deviceDeadline" placeholder="请输入设备维保到期剩余" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备上次维保时间" label-width="124px">
        <el-date-picker v-model="daterangeDeviceLasttime" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="设备所属车间" prop="deviceWorkshop" label-width="110px">
        <el-select v-model="queryParams.deviceWorkshop" placeholder="请选择设备所属车间" clearable>
          <el-option v-for="dict in dict.type.product_order_workshop" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="保养部位设定" prop="deviceCheckpoint" label-width="110px">
        <el-input v-model="queryParams.deviceCheckpoint" placeholder="请输入保养部位设定" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item style="margin-left:30px;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:maintenance:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:maintenance:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:maintenance:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:maintenance:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maintenanceList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备名称" align="center" prop="deviceName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_name" :value="scope.row.deviceName" />
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="deviceModel" />
      <el-table-column label="设备负责人" align="center" prop="deviceManager" />
      <el-table-column label="设备保养周期(天)" align="center" prop="deviceRound" />
      <el-table-column label="设备维保到期剩余(天)" align="center" prop="deviceDeadline" />
      <el-table-column label="设备上次维保时间" align="center" prop="deviceLasttime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deviceLasttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备所属车间" align="center" prop="deviceWorkshop">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_order_workshop" :value="scope.row.deviceWorkshop" />
        </template>
      </el-table-column>
      <el-table-column label="保养部位设定" align="center" prop="deviceCheckpoint" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:maintenance:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:maintenance:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改维保对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-select v-model="form.deviceName" placeholder="请选择设备名称">
            <el-option v-for="dict in dict.type.device_name" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceModel">
          <el-input v-model="form.deviceModel" placeholder="请输入设备类型" />
        </el-form-item>
        <el-form-item label="设备负责人" prop="deviceManager">
          <el-input v-model="form.deviceManager" placeholder="请输入设备负责人" />
        </el-form-item>
        <el-form-item label="设备保养周期" prop="deviceRound">
          <el-input v-model="form.deviceRound" placeholder="请输入设备保养周期" />
        </el-form-item>
        <el-form-item label="设备上次维保时间" prop="deviceLasttime">
          <el-date-picker clearable v-model="form.deviceLasttime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择设备上次维保时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备所属车间" prop="deviceWorkshop">
          <el-select v-model="form.deviceWorkshop" placeholder="请选择设备所属车间">
            <el-option v-for="dict in dict.type.product_order_workshop" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保养部位设定" prop="deviceCheckpoint">
          <el-input v-model="form.deviceCheckpoint" placeholder="请输入保养部位设定" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listMaintenance,
    getMaintenance,
    delMaintenance,
    addMaintenance,
    updateMaintenance
  } from "@/api/system/maintenance";

  export default {
    name: "Maintenance",
    dicts: ['device_name', 'product_order_workshop'],
    data() {
      return {
        // 遮罩层
        loading: true,
        ids: [],
        // 选中数组
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 维保表格数据
        maintenanceList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 保养部位设定时间范围
        daterangeDeviceLasttime: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deviceName: null,
          deviceModel: null,
          deviceManager: null,
          deviceRound: null,
          deviceDeadline: null,
          deviceLasttime: null,
          deviceWorkshop: null,
          deviceCheckpoint: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        times: null,
        timer: undefined,
        nowTime: new Date(),
      };
    },
    created() {
      // 显示时间
      this.timer = setInterval(() => {
        this.nowTime = new Date().toLocaleString();
      });
      this.getList();
      this.times = setInterval(() => {
        this.getList();
      }, 1000 * 10);
    },

    // 销毁定时器
    beforeDestroy() {
      if (this.timer) {
        clearInterval(this.timer);
      }
    },
    destroyed() { //销毁
      clearInterval(this.times);
    },
    methods: {
      /** 查询维保列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeDeviceLasttime && '' != this.daterangeDeviceLasttime) {
          this.queryParams.params["beginDeviceLasttime"] = this.daterangeDeviceLasttime[0];
          this.queryParams.params["endDeviceLasttime"] = this.daterangeDeviceLasttime[1];
        }
        listMaintenance(this.queryParams).then(response => {
          this.maintenanceList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          deviceName: null,
          deviceModel: null,
          deviceManager: null,
          deviceRound: null,
          deviceDeadline: null,
          deviceLasttime: null,
          deviceWorkshop: null,
          deviceCheckpoint: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeDeviceLasttime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加维保";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getMaintenance(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改维保";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateMaintenance(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMaintenance(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除维保编号为"' + ids + '"的数据项？').then(function() {
          return delMaintenance(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/maintenance/export', {
          ...this.queryParams
        }, `maintenance_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
